﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LantinxShare.LBS;
using System.Collections.Generic;
using LantinxShare.Common;
using LantinxShare.LBS.Location;

namespace LantinxLBS.Common
{
    public class DataHelper
    {
        #region User
        public static List<GUser> ConvertToUser(ResultObj e)
        {
            List<GUser> userList = new List<GUser>();
            List<string[]> list = e.ResultList;

            foreach (var item in list)
            {
                userList.Add(new GUser()
                {
                    Id = int.Parse(item[0]),
                    UserName = item[1],
                    Password = item[2],
                    UserAlias = item[3],
                    Phone = item[4],
                    Role = new GRole()
                    {
                        Id = int.Parse(item[5]),
                        RoleId = int.Parse(item[6]),
                        RoleName = item[7]
                    }
                });
            }
            return userList;
        }

        public static void LoadUser(Action<List<GUser>> action)
        {

            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_GET_USER, (e) =>
            {
                List<GUser> userList = DataHelper.ConvertToUser(e);

                action(userList);
            });

        }

        public static void SaveUser(String usreName, String userAlias, String password, String phone, String roleId, Action<List<GUser>> action)
        {

            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_SET_USER,
                new Dictionary<string, string>(){
                {"UserName",usreName},
                {"UserAlias",userAlias},
                {"Password",password},
                {"Phone",phone},
                {"RoleId",roleId}
                }, (e) =>
                {
                    List<GUser> userList = ConvertToUser(e);
                    action(userList);
                });
        }

        #endregion

        #region Mark

        public static void LoadMark(Action<List<GMark>> action)
        {
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_GET_MARK, (e) =>
            {
                List<GMark> markList = new List<GMark>();
                List<string[]> list = e.ResultList;

                foreach (var item in list)
                {
                    markList.Add(new GMark()
                    {
                        Id = int.Parse(item[0]),
                        MarkName = item[1],
                        Address = item[2],
                        Description = item[3],
                        Longitude = double.Parse(item[4]),
                        Latitude = double.Parse(item[5]),
                        Altitude = double.Parse(item[6]),
                        Radius = double.Parse(item[7]),
                        West = double.Parse(item[8]),
                        South = double.Parse(item[9]),
                        East = double.Parse(item[10]),
                        North = double.Parse(item[11])
                    });
                }

                action(markList);
            });
        }

        #endregion

        #region WorkItemType
        public static void LoadWorkItemType(Action<List<GWorkItemType>> action)
        {
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_GET_WORK_ITEM_TYPE, (e) =>
            {
                List<GWorkItemType> markList = new List<GWorkItemType>();
                List<string[]> list = e.ResultList;

                foreach (var item in list)
                {
                    markList.Add(new GWorkItemType()
                    {
                        Id = int.Parse(item[0]),
                        WorkItemName = item[1],
                        WorkType = int.Parse(item[2]),
                        WorkLevel = int.Parse(item[3])
                    });
                }

                action(markList);
            });
        }

        public static void SaveWorkItemType(string workItemName,String workType,String workLevel, Action<bool> action)
        {
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_SET_WORK_ITEM_TYPE, new Dictionary<string, string>(){
                {"WorkItemName",workItemName},
                 {"WorkType",workType},
                 {"WorkLevel",workLevel}
                }, (e) =>
            {

                action(bool.Parse(e.Result));
            });
        }

        #endregion
        #region GTask
        public static void SaveTask(GUser user, GMark mark, DateTime startTime, DateTime endTime, String workItems, Action<bool> action)
        {
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_SET_WORKITEM,
               new Dictionary<string, string>(){
                {"UserId",user.Id.ToString()},
                {"MarkId",mark.Id.ToString()},
                {"StartDate",startTime.ToString("yyyyMMddHHmm00")},
                {"EndDate",endTime.ToString("yyyyMMddHHmm00")},
                {"WorkItems",workItems}
                }, (e) =>
                {

                    action(bool.Parse(e.Result));
                });
        }
        #endregion

        #region UserLocation
        public static void LoadUserLocationByDate(string userId, DateTime dateTime, Action<List<GUserLocation>> action)
        {
            PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_GET_LOCATION_DATE,
                new Dictionary<string, string>(){
                    {"userId",userId},
                {"Date",dateTime.ToString("yyyy-MM-dd")}}, (e) =>
            {
                List<GUserLocation> localList = new List<GUserLocation>();
                List<string[]> list = e.ResultList;

                foreach (var item in list)
                {
                    GUserLocation location = new GUserLocation()
                        {
                            Id = int.Parse(item[0]),
                            User = new GUser()
                            {
                                Id = int.Parse(item[1])
                            },
                            Location = new GLocation()
                            {

                                Title = item[2],
                                Location = new GPoint()
                                {
                                    Longitude = double.Parse(item[3]),
                                    Latitude = double.Parse(item[4])
                                },
                                GTimeStamp = CDateStampEx.ConvertToDateTime(item[5]),
                                Speed = double.Parse(item[6]),
                                Altitude = double.Parse(item[7]),
                            }
                        };

                    localList.Add(location);
                }

                action(localList);
            });
        }
        #endregion
    }// end for class
    public class CDateStampEx
    {
        public static DateTime ConvertToDateTime(string dateTime)
        {
            return new DateTime(
                                            int.Parse(dateTime.Substring(0, 4)),
                                            int.Parse(dateTime.Substring(4, 2)),
                                            int.Parse(dateTime.Substring(6, 2)),
                                            int.Parse(dateTime.Substring(8, 2)),
                                            int.Parse(dateTime.Substring(10, 2)), 0);
        }
        public static DateTime ConvertToDateTime(long dateTime)
        {
            return ConvertToDateTime(dateTime.ToString());
        }
    }
}
